/*测试数据*/
var testdata=[
	{
		id: 1,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 2,
		userId: 23222,
		phone: 18817802221,
		realName: 'lisd',
		address: '上海市浦东新区2',
		shoppingAddress: '上海市浦东新区2',
		createTime: 1502937009,
		userMoney: 23662,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 3,
		userId: 2322,
		phone: 18817802241,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 4,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	}
];

$(function(){
	getTableData();
});
/*获取表格数据*/
function getTableData(){
	/*var backData=testdata;//[];
	tableInit(backData);*/
    $.post(
        gobalServer+"user/selectAll",
        {},
        function(backData){
            if(backData.statusCode==200){
                var backData=backData.commonList;//[];
                tableInit(backData);
            }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
    );

};
/*表格初始化*/
function tableInit(backData){
	var objectArray=[];
    if(backData.length>0){
        objectArray=dataToJson(backData);
    }
    $('#stockTable').bootstrapTable('destroy').bootstrapTable({
        toolbar: '#toolbar',                //工具按钮用哪个容器
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        data: objectArray,
        pageList: [5, 10, 20, 40, 100, 200]/*,
        rowStyle: function rowStyle(row,index) {
            if(row.mark!=null && row.mark>0){
                return {
                    css:{
                        "background-color": "#ffc930",
                        "color": "#fff"
                    }
                };
            }else{
                return {
                    css:{

                    }
                };
            }
        }*/
    });
};
/*表格数据封装*/
function dataToJson(dataResult) {
    var objectArray=new Array();
    $.each(dataResult,function(index,item){
    	var createTime="--";
    	if(item.createTime!=null){
        	createTime=formatDateTimeYMDHMS(item.createTime);
        }
        var permission="";
    	if(item.permission==7001){
            permission="管理员";
		}
        if(item.permission==7002){
            permission="客户";
        }
        var userMoney="";
        if(item.userMoney!=null){
        	userMoney="<b class='text-red'>￥"+item.userMoney+"</a>";
        }
        var taxtail="";
        taxtail="<a class='btn-a-common' href='javascript:;' tgid='"+item.userId+"' onclick='getMesDetail(this)' >查看</a>";
        var opdetail="";
        opdetail="<a class='btn-a-common' href='"+gobalWeb+"finance/userfinance.html?tgid="+item.userId+"' >查看</a>";
        
         var orderdetail="";
        orderdetail="<a class='btn-a-common' href='"+gobalWeb+"orders/orders.html?tgid="+item.userId+"' >查看</a>";
        
        var operate="";
        operate="<a class='btn-a-common' href='"+gobalWeb+"user/usermg.html?tgid="+item.userId+"'>修改</a>";
        operate+="<a class='btn-a-common' href='javascript:;' tgid='"+item.userId+"' onclick='deleteItemData(this)'>删除</a>";
        var arr={
        	"id":item.id,
        	"userId":item.userId,
            "phone":item.phone,
            "realName":item.realName,
			"permission":permission,
            "address":item.address,
            "shoppingAddress":item.shoppingAddress,
            "userMoney":userMoney,
            "taxtail":taxtail,
            "opdetail":opdetail,
            "orderdetail":orderdetail,
            "createTime":createTime,
            "operate":operate
        };
        objectArray.push(arr);
    });
    return objectArray;
};
/*删除数据*/
function deleteItemData(obj){
	var tgid= $(obj).attr("tgid");
	if(tgid!=null && tgid!=""){
		var msg = "您真的确定要删除吗？\n\n删除后无法撤回！";
		if (confirm(msg)==true){
			/*consoleMes($("#manageContent"),2000,"success","数据已经成功删除！");
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);*/
            $.post(
                gobalServer+"user/delete",
                {"userId":tgid},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success",backData.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                    }
                }
            );

		}
	}else{
		consoleMes($("#manageContent"),2000,"warning","删除失败，请刷新页面重试！");//success、info、warning、danger
	}
};
/*查看收支详情*/
function getMesDetail(obj){
    var tgid= $(obj).attr("tgid");
	if(tgid!=null && tgid!=""){
		/*var datarows=testdata[0];
		if(datarows!=null){
			$("#taxType").text(datarows.taxType);
			$("#taxCompany").text(datarows.taxCompany);
			$("#taxId").text(datarows.taxId);
			$("#taxPhone").text(datarows.taxPhone);
			$("#taxAddress").text(datarows.taxAddress);
			$("#taxBankId").text(datarows.taxBankId);
			$("#taxBankName").text(datarows.taxBankName);
			$("#stockModal").modal("show");
		}else{
			consoleMes($("#manageContent"),2000,"warning","无法获取数据，请刷新重试！");
		}*/
        $.post(
            gobalServer+"user/selectOne",
            {"userId":tgid},
            function(backData){
                if(backData.statusCode==200){
					var datarows=backData.object;
					 if(datarows!=null){
						 $("#taxType").text(datarows.taxType);
						 $("#taxCompany").text(datarows.taxCompany);
						 $("#taxId").text(datarows.taxId);
						 $("#taxPhone").text(datarows.taxPhone);
						 $("#taxAddress").text(datarows.taxAddress);
						 $("#taxBankId").text(datarows.taxBankId);
						 $("#taxBankName").text(datarows.taxBankName);
						 $("#stockModal").modal("show");
					 }else{
					 	consoleMes($("#manageContent"),2000,"warning","无法获取数据，请刷新重试！");
					 }
                }else{
                    consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                }
            }
        );

	}else{
		consoleMes($("#manageContent"),2000,"warning","无法查看，请刷新页面重试！");//success、info、warning、danger
	}
};
